summaryrefslogtreecommitdiffstats
path: root/src/video_core/surface.cpp
blob: cb51529e4d4b9939837ccff4a37d4abc1449e0df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/common_types.h"
#include "common/math_util.h"
#include "video_core/surface.h"

namespace VideoCore::Surface {

SurfaceTarget SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type) {
    switch (texture_type) {
    case Tegra::Texture::TextureType::Texture1D:
        return SurfaceTarget::Texture1D;
    case Tegra::Texture::TextureType::Texture1DBuffer:
        return SurfaceTarget::TextureBuffer;
    case Tegra::Texture::TextureType::Texture2D:
    case Tegra::Texture::TextureType::Texture2DNoMipmap:
        return SurfaceTarget::Texture2D;
    case Tegra::Texture::TextureType::Texture3D:
        return SurfaceTarget::Texture3D;
    case Tegra::Texture::TextureType::TextureCubemap:
        return SurfaceTarget::TextureCubemap;
    case Tegra::Texture::TextureType::TextureCubeArray:
        return SurfaceTarget::TextureCubeArray;
    case Tegra::Texture::TextureType::Texture1DArray:
        return SurfaceTarget::Texture1DArray;
    case Tegra::Texture::TextureType::Texture2DArray:
        return SurfaceTarget::Texture2DArray;
    default:
        LOG_CRITICAL(HW_GPU, "Unimplemented texture_type={}", texture_type);
        ASSERT(false);
        return SurfaceTarget::Texture2D;
    }
}

bool SurfaceTargetIsLayered(SurfaceTarget target) {
    switch (target) {
    case SurfaceTarget::Texture1D:
    case SurfaceTarget::TextureBuffer:
    case SurfaceTarget::Texture2D:
    case SurfaceTarget::Texture3D:
        return false;
    case SurfaceTarget::Texture1DArray:
    case SurfaceTarget::Texture2DArray:
    case SurfaceTarget::TextureCubemap:
    case SurfaceTarget::TextureCubeArray:
        return true;
    default:
        LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
        ASSERT(false);
        return false;
    }
}

bool SurfaceTargetIsArray(SurfaceTarget target) {
    switch (target) {
    case SurfaceTarget::Texture1D:
    case SurfaceTarget::TextureBuffer:
    case SurfaceTarget::Texture2D:
    case SurfaceTarget::Texture3D:
    case SurfaceTarget::TextureCubemap:
        return false;
    case SurfaceTarget::Texture1DArray:
    case SurfaceTarget::Texture2DArray:
    case SurfaceTarget::TextureCubeArray:
        return true;
    default:
        LOG_CRITICAL(HW_GPU, "Unimplemented surface_target={}", target);
        ASSERT(false);
        return false;
    }
}

PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format) {
    switch (format) {
    case Tegra::DepthFormat::Z24_UNORM_S8_UINT:
        return PixelFormat::S8_UINT_D24_UNORM;
    case Tegra::DepthFormat::S8Z24_UNORM:
        return PixelFormat::D24_UNORM_S8_UINT;
    case Tegra::DepthFormat::Z32_FLOAT:
        return PixelFormat::D32_FLOAT;
    case Tegra::DepthFormat::Z16_UNORM:
        return PixelFormat::D16_UNORM;
    case Tegra::DepthFormat::S8_UINT:
        return PixelFormat::S8_UINT;
    case Tegra::DepthFormat::Z32_FLOAT_X24S8_UINT:
        return PixelFormat::D32_FLOAT_S8_UINT;
    default:
        UNIMPLEMENTED_MSG("Unimplemented format={}", format);
        return PixelFormat::S8_UINT_D24_UNORM;
    }
}

PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format) {
    switch (format) {
    case Tegra::RenderTargetFormat::R32G32B32A32_FLOAT:
    case Tegra::RenderTargetFormat::R32G32B32X32_FLOAT:
        return PixelFormat::R32G32B32A32_FLOAT;
    case Tegra::RenderTargetFormat::R32G32B32A32_SINT:
    case Tegra::RenderTargetFormat::R32G32B32X32_SINT:
        return PixelFormat::R32G32B32A32_SINT;
    case Tegra::RenderTargetFormat::R32G32B32A32_UINT:
    case Tegra::RenderTargetFormat::R32G32B32X32_UINT:
        return PixelFormat::R32G32B32A32_UINT;
    case Tegra::RenderTargetFormat::R16G16B16A16_UNORM:
        return PixelFormat::R16G16B16A16_UNORM;
    case Tegra::RenderTargetFormat::R16G16B16A16_SNORM:
        return PixelFormat::R16G16B16A16_SNORM;
    case Tegra::RenderTargetFormat::R16G16B16A16_SINT:
        return PixelFormat::R16G16B16A16_SINT;
    case Tegra::RenderTargetFormat::R16G16B16A16_UINT:
        return PixelFormat::R16G16B16A16_UINT;
    case Tegra::RenderTargetFormat::R16G16B16A16_FLOAT:
        return PixelFormat::R16G16B16A16_FLOAT;
    case Tegra::RenderTargetFormat::R32G32_FLOAT:
        return PixelFormat::R32G32_FLOAT;
    case Tegra::RenderTargetFormat::R32G32_SINT:
        return PixelFormat::R32G32_SINT;
    case Tegra::RenderTargetFormat::R32G32_UINT:
        return PixelFormat::R32G32_UINT;
    case Tegra::RenderTargetFormat::R16G16B16X16_FLOAT:
        return PixelFormat::R16G16B16X16_FLOAT;
    case Tegra::RenderTargetFormat::A8R8G8B8_UNORM:
    case Tegra::RenderTargetFormat::X8R8G8B8_UNORM:
        return PixelFormat::B8G8R8A8_UNORM;
    case Tegra::RenderTargetFormat::A8R8G8B8_SRGB:
    case Tegra::RenderTargetFormat::X8R8G8B8_SRGB:
        return PixelFormat::B8G8R8A8_SRGB;
    case Tegra::RenderTargetFormat::A2B10G10R10_UNORM:
        return PixelFormat::A2B10G10R10_UNORM;
    case Tegra::RenderTargetFormat::A2B10G10R10_UINT:
        return PixelFormat::A2B10G10R10_UINT;
    case Tegra::RenderTargetFormat::A2R10G10B10_UNORM:
        return PixelFormat::A2R10G10B10_UNORM;
    case Tegra::RenderTargetFormat::A8B8G8R8_UNORM:
    case Tegra::RenderTargetFormat::X8B8G8R8_UNORM:
        return PixelFormat::A8B8G8R8_UNORM;
    case Tegra::RenderTargetFormat::A8B8G8R8_SRGB:
    case Tegra::RenderTargetFormat::X8B8G8R8_SRGB:
        return PixelFormat::A8B8G8R8_SRGB;
    case Tegra::RenderTargetFormat::A8B8G8R8_SNORM:
        return PixelFormat::A8B8G8R8_SNORM;
    case Tegra::RenderTargetFormat::A8B8G8R8_SINT:
        return PixelFormat::A8B8G8R8_SINT;
    case Tegra::RenderTargetFormat::A8B8G8R8_UINT:
        return PixelFormat::A8B8G8R8_UINT;
    case Tegra::RenderTargetFormat::R16G16_UNORM:
        return PixelFormat::R16G16_UNORM;
    case Tegra::RenderTargetFormat::R16G16_SNORM:
        return PixelFormat::R16G16_SNORM;
    case Tegra::RenderTargetFormat::R16G16_SINT:
        return PixelFormat::R16G16_SINT;
    case Tegra::RenderTargetFormat::R16G16_UINT:
        return PixelFormat::R16G16_UINT;
    case Tegra::RenderTargetFormat::R16G16_FLOAT:
        return PixelFormat::R16G16_FLOAT;
    case Tegra::RenderTargetFormat::B10G11R11_FLOAT:
        return PixelFormat::B10G11R11_FLOAT;
    case Tegra::RenderTargetFormat::R32_SINT:
        return PixelFormat::R32_SINT;
    case Tegra::RenderTargetFormat::R32_UINT:
        return PixelFormat::R32_UINT;
    case Tegra::RenderTargetFormat::R32_FLOAT:
        return PixelFormat::R32_FLOAT;
    case Tegra::RenderTargetFormat::R5G6B5_UNORM:
        return PixelFormat::R5G6B5_UNORM;
    case Tegra::RenderTargetFormat::A1R5G5B5_UNORM:
    case Tegra::RenderTargetFormat::X1R5G5B5_UNORM:
        return PixelFormat::A1R5G5B5_UNORM;
    case Tegra::RenderTargetFormat::R8G8_UNORM:
        return PixelFormat::R8G8_UNORM;
    case Tegra::RenderTargetFormat::R8G8_SNORM:
        return PixelFormat::R8G8_SNORM;
    case Tegra::RenderTargetFormat::R8G8_SINT:
        return PixelFormat::R8G8_SINT;
    case Tegra::RenderTargetFormat::R8G8_UINT:
        return PixelFormat::R8G8_UINT;
    case Tegra::RenderTargetFormat::R16_UNORM:
        return PixelFormat::R16_UNORM;
    case Tegra::RenderTargetFormat::R16_SNORM:
        return PixelFormat::R16_SNORM;
    case Tegra::RenderTargetFormat::R16_SINT:
        return PixelFormat::R16_SINT;
    case Tegra::RenderTargetFormat::R16_UINT:
        return PixelFormat::R16_UINT;
    case Tegra::RenderTargetFormat::R16_FLOAT:
        return PixelFormat::R16_FLOAT;
    case Tegra::RenderTargetFormat::R8_UNORM:
        return PixelFormat::R8_UNORM;
    case Tegra::RenderTargetFormat::R8_SNORM:
        return PixelFormat::R8_SNORM;
    case Tegra::RenderTargetFormat::R8_SINT:
        return PixelFormat::R8_SINT;
    case Tegra::RenderTargetFormat::R8_UINT:
        return PixelFormat::R8_UINT;
    default:
        UNIMPLEMENTED_MSG("Unimplemented format={}", format);
        return PixelFormat::A8B8G8R8_UNORM;
    }
}

PixelFormat PixelFormatFromGPUPixelFormat(Service::android::PixelFormat format) {
    switch (format) {
    case Service::android::PixelFormat::Rgba8888:
        return PixelFormat::A8B8G8R8_UNORM;
    case Service::android::PixelFormat::Rgb565:
        return PixelFormat::R5G6B5_UNORM;
    case Service::android::PixelFormat::Bgra8888:
        return PixelFormat::B8G8R8A8_UNORM;
    default:
        UNIMPLEMENTED_MSG("Unimplemented format={}", format);
        return PixelFormat::A8B8G8R8_UNORM;
    }
}

SurfaceType GetFormatType(PixelFormat pixel_format) {
    if (pixel_format < PixelFormat::MaxColorFormat) {
        return SurfaceType::ColorTexture;
    }
    if (pixel_format < PixelFormat::MaxDepthFormat) {
        return SurfaceType::Depth;
    }
    if (pixel_format < PixelFormat::MaxStencilFormat) {
        return SurfaceType::Stencil;
    }
    if (pixel_format < PixelFormat::MaxDepthStencilFormat) {
        return SurfaceType::DepthStencil;
    }

    // TODO(Subv): Implement the other formats
    ASSERT(false);

    return SurfaceType::Invalid;
}

bool IsPixelFormatASTC(PixelFormat format) {
    switch (format) {
    case PixelFormat::ASTC_2D_4X4_UNORM:
    case PixelFormat::ASTC_2D_5X4_UNORM:
    case PixelFormat::ASTC_2D_5X5_UNORM:
    case PixelFormat::ASTC_2D_8X8_UNORM:
    case PixelFormat::ASTC_2D_8X5_UNORM:
    case PixelFormat::ASTC_2D_4X4_SRGB:
    case PixelFormat::ASTC_2D_5X4_SRGB:
    case PixelFormat::ASTC_2D_5X5_SRGB:
    case PixelFormat::ASTC_2D_8X8_SRGB:
    case PixelFormat::ASTC_2D_8X5_SRGB:
    case PixelFormat::ASTC_2D_10X8_UNORM:
    case PixelFormat::ASTC_2D_10X8_SRGB:
    case PixelFormat::ASTC_2D_6X6_UNORM:
    case PixelFormat::ASTC_2D_6X6_SRGB:
    case PixelFormat::ASTC_2D_10X6_UNORM:
    case PixelFormat::ASTC_2D_10X6_SRGB:
    case PixelFormat::ASTC_2D_10X5_UNORM:
    case PixelFormat::ASTC_2D_10X5_SRGB:
    case PixelFormat::ASTC_2D_10X10_UNORM:
    case PixelFormat::ASTC_2D_10X10_SRGB:
    case PixelFormat::ASTC_2D_12X10_UNORM:
    case PixelFormat::ASTC_2D_12X10_SRGB:
    case PixelFormat::ASTC_2D_12X12_UNORM:
    case PixelFormat::ASTC_2D_12X12_SRGB:
    case PixelFormat::ASTC_2D_8X6_UNORM:
    case PixelFormat::ASTC_2D_8X6_SRGB:
    case PixelFormat::ASTC_2D_6X5_UNORM:
    case PixelFormat::ASTC_2D_6X5_SRGB:
        return true;
    default:
        return false;
    }
}

bool IsPixelFormatSRGB(PixelFormat format) {
    switch (format) {
    case PixelFormat::A8B8G8R8_SRGB:
    case PixelFormat::B8G8R8A8_SRGB:
    case PixelFormat::BC1_RGBA_SRGB:
    case PixelFormat::BC2_SRGB:
    case PixelFormat::BC3_SRGB:
    case PixelFormat::BC7_SRGB:
    case PixelFormat::ASTC_2D_4X4_SRGB:
    case PixelFormat::ASTC_2D_8X8_SRGB:
    case PixelFormat::ASTC_2D_8X5_SRGB:
    case PixelFormat::ASTC_2D_5X4_SRGB:
    case PixelFormat::ASTC_2D_5X5_SRGB:
    case PixelFormat::ASTC_2D_10X6_SRGB:
    case PixelFormat::ASTC_2D_10X8_SRGB:
    case PixelFormat::ASTC_2D_6X6_SRGB:
    case PixelFormat::ASTC_2D_10X5_SRGB:
    case PixelFormat::ASTC_2D_10X10_SRGB:
    case PixelFormat::ASTC_2D_12X12_SRGB:
    case PixelFormat::ASTC_2D_12X10_SRGB:
    case PixelFormat::ASTC_2D_8X6_SRGB:
    case PixelFormat::ASTC_2D_6X5_SRGB:
        return true;
    default:
        return false;
    }
}

bool IsPixelFormatInteger(PixelFormat format) {
    switch (format) {
    case PixelFormat::A8B8G8R8_SINT:
    case PixelFormat::A8B8G8R8_UINT:
    case PixelFormat::A2B10G10R10_UINT:
    case PixelFormat::R8_SINT:
    case PixelFormat::R8_UINT:
    case PixelFormat::R16G16B16A16_SINT:
    case PixelFormat::R16G16B16A16_UINT:
    case PixelFormat::R32G32B32A32_UINT:
    case PixelFormat::R32G32B32A32_SINT:
    case PixelFormat::R32G32_SINT:
    case PixelFormat::R16_UINT:
    case PixelFormat::R16_SINT:
    case PixelFormat::R16G16_UINT:
    case PixelFormat::R16G16_SINT:
    case PixelFormat::R8G8_SINT:
    case PixelFormat::R8G8_UINT:
    case PixelFormat::R32G32_UINT:
    case PixelFormat::R32_UINT:
    case PixelFormat::R32_SINT:
        return true;
    default:
        return false;
    }
}

bool IsPixelFormatSignedInteger(PixelFormat format) {
    switch (format) {
    case PixelFormat::A8B8G8R8_SINT:
    case PixelFormat::R8_SINT:
    case PixelFormat::R16G16B16A16_SINT:
    case PixelFormat::R32G32B32A32_SINT:
    case PixelFormat::R32G32_SINT:
    case PixelFormat::R16_SINT:
    case PixelFormat::R16G16_SINT:
    case PixelFormat::R8G8_SINT:
    case PixelFormat::R32_SINT:
        return true;
    default:
        return false;
    }
}

size_t PixelComponentSizeBitsInteger(PixelFormat format) {
    switch (format) {
    case PixelFormat::A8B8G8R8_SINT:
    case PixelFormat::A8B8G8R8_UINT:
    case PixelFormat::R8_SINT:
    case PixelFormat::R8_UINT:
    case PixelFormat::R8G8_SINT:
    case PixelFormat::R8G8_UINT:
        return 8;
    case PixelFormat::A2B10G10R10_UINT:
        return 10;
    case PixelFormat::R16G16B16A16_SINT:
    case PixelFormat::R16G16B16A16_UINT:
    case PixelFormat::R16_UINT:
    case PixelFormat::R16_SINT:
    case PixelFormat::R16G16_UINT:
    case PixelFormat::R16G16_SINT:
        return 16;
    case PixelFormat::R32G32B32A32_UINT:
    case PixelFormat::R32G32B32A32_SINT:
    case PixelFormat::R32G32_SINT:
    case PixelFormat::R32G32_UINT:
    case PixelFormat::R32_UINT:
    case PixelFormat::R32_SINT:
        return 32;
    default:
        return 0;
    }
}

std::pair<u32, u32> GetASTCBlockSize(PixelFormat format) {
    return {DefaultBlockWidth(format), DefaultBlockHeight(format)};
}

u64 EstimatedDecompressedSize(u64 base_size, PixelFormat format) {
    constexpr u64 RGBA8_PIXEL_SIZE = 4;
    const u64 base_block_size = static_cast<u64>(DefaultBlockWidth(format)) *
                                static_cast<u64>(DefaultBlockHeight(format)) * RGBA8_PIXEL_SIZE;
    return (base_size * base_block_size) / BytesPerBlock(format);
}

} // namespace VideoCore::Surface